import { getMinWidth } from './utils'
import { useColor } from '/@/hooks/color'

/**
 * 构造 tableColumn props
 * @param prop column.prop
 * @param label column.label
 * @param options tableColumn的其他属性
 * @returns tableColumn props
 */
export function useTableColumnProps<
  T extends Record<string, any>,
  P extends keyof T
>(
  prop: P,
  label: string,
  options: UseTableColumnProps<any, T, P> = {}
): TableColumnProps<T, P> {
  const innerOptions: UseTableColumnProps<any, T, P> = {}
  // 如果设置了width则默认minWidth为width，否则保障minWidth = 200
  if ('width' in options) {
    innerOptions.minWidth = options.width
  } else if (!('minWidth' in options)) {
    innerOptions.minWidth = getMinWidth(label.length)
  }

  if (!options?.component) {
    innerOptions.formatter = (row, column, cellValue, index) => {
      let format =
        'formatter' in options ?
          options.formatter!(row, column, cellValue, index) :
          row[prop]
      // 确定
      if (format) {
        switch (typeof format) {
          case 'number':
          case 'string': {
            format = useColor(format, options.color, row)
          }
        }
      }
      return format || format === 0 ? format : '-'
    }
  }

  return {
    showOverflowTooltip: true,
    ...options,
    ...innerOptions,
    prop,
    label
  }
}
